package com.csi.jf.crucible.api.util;

/**
 * 
 * @Title: ApiUtils.java
 * @Description: <br>
 *               <br>
 * @Company: ICSS
 * @Created on 2014-7-8 下午3:20:29
 * @author FengHaiBing
 */
public class ApiUtils {
	
    /**
     * 在crucible, sonar中使用的项目名称最大长度
     */
    public static final int PRJ_MAX_LENGTH = 8;
    
    /**
     * 项目在crucible中的后缀
     */
    public static final String PRJ_CRUCIBLE_PREFIX = "cru";
    
    /**
     * repository在crucible中的后缀
     */
    public static final String REPO_CRUCIBLE_PREFIX = "rp";
    
    /**
     * 项目在sonar中的后缀
     */
    public static final String PRJ_SONAR_PREFIX = "snr";
    

    /**
     * 基于projectId得到sonarKey
     * @param projectId
     * @return
     * @Created on 2014-7-8 下午3:07:18
     * @author FengHaiBing
     */
    public static final String getSonarKeyByProjectId(Long projectId) {
    	return PRJ_SONAR_PREFIX + projectId;
    }
    
    /**
     * 基于projectId得到crucible project Key
     * @param projectId
     * @return
     * @Created on 2014-7-8 下午3:11:18
     * @author FengHaiBing
     */
    public static final String getCruKeyByProjectId(Long projectId) {
    	return PRJ_CRUCIBLE_PREFIX + projectId;
    }
    
    /**
     * 基于projectName得到crucible projectName
     * @param projectName
     * @return
     * @Created on 2014-7-8 下午3:15:14
     * @author FengHaiBing
     */
    public static final String getCruProjectName(String projectName) {
    	return StringUtils.getPrefix(projectName, PRJ_MAX_LENGTH) + "_" + PRJ_CRUCIBLE_PREFIX;
    }
    
    /**
     * 
     * @param projectName
     * @return
     * @Created on 2014-7-8 下午3:17:44
     * @author FengHaiBing
     */
    public static final String getCruRepo(String projectName) {
    	return StringUtils.getPrefix(projectName, PRJ_MAX_LENGTH) + "_" + REPO_CRUCIBLE_PREFIX;
    }
    
    /**
     * 格式化参数
     * @param url
     * @param args
     * @return
     */
    public static String formatUrl(String url,String[] names,String[] args){
        if(names!=null&&args!=null){
            if(names.length!=args.length){
                throw new RuntimeException("The length of args is not equals names.");
            }
            for(int i=0;i<names.length;i++){
                url = url.replace("{"+names[i]+"}", args[i]);
            }
        }
        return url;
    }
    
    public static void main(String[] args) {
    	String projectName = "广东中烟决策支持 ";
    	System.out.println(getCruRepo(projectName));
    }
    
}
